﻿/*************************************************************************
 * 
 * Filename :flashconfig
 * 
 * steven hu   2010/3/15 9:46:22
 *  
 * http://www.cnblogs.com/huxj
 *   
 * 
 * Change History:
 * 
 * 
**************************************************************************/


using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;

namespace RicciWebsite.Utils
{


    public class flashconfigHelper
    {
        private static string configPath = HttpContext.Current.Server.MapPath("~/webpic/xml/bcastr.xml");

        /// <summary>
        /// 读取Flash配置
        /// </summary>
        /// <returns></returns>
        public static flashconfig Load()
        {
            flashconfig config = new flashconfig();

            XmlDocument doc = new XmlDocument();
            doc.Load(configPath);

            config.AutoPlayTime = Hxj.Common.ConvertHelper.ToInt32(doc.GetElementsByTagName("bcaster").Item(0).Attributes["autoPlayTime"].Value, 10);

            List<flashconfigitem> list = new List<flashconfigitem>();

            XmlNodeList nodes = doc.GetElementsByTagName("item");


            foreach (XmlNode node in nodes)
            {
                flashconfigitem item = new flashconfigitem();

                item.Id = Hxj.Common.ConvertHelper.ToInt32(node.Attributes["id"].Value, 1);
                item.Item_url = node.Attributes["item_url"].Value;
                item.Link = node.Attributes["link"].Value;
                item.Title = node.Attributes["title"].Value;

                list.Add(item);
            }

            config.Items = list;

            return config;
        }

        /// <summary>
        /// 设置Flash配置
        /// </summary>
        /// <param name="config"></param>
        public static void Set(flashconfig config)
        {
            if (null == config)
                return;

            XmlDocument doc = new XmlDocument();
            doc.Load(configPath);

            XmlNode rootNode = doc.GetElementsByTagName("bcaster").Item(0);
            rootNode.RemoveAll();


            XmlAttribute att = doc.CreateAttribute("autoPlayTime");
            att.Value = config.AutoPlayTime.ToString();
            rootNode.Attributes.Append(att);

            foreach (flashconfigitem item in config.Items)
            {
                XmlElement xe = doc.CreateElement("item");
                XmlAttribute xat = doc.CreateAttribute("item_url");
                xat.Value = item.Item_url;
                xe.Attributes.Append(xat);

                xat = doc.CreateAttribute("link");
                xat.Value = item.Link;
                xe.Attributes.Append(xat);

                xat = doc.CreateAttribute("id");
                xat.Value = item.Id.ToString();
                xe.Attributes.Append(xat);

                xat = doc.CreateAttribute("title");
                xat.Value = item.Title;
                xe.Attributes.Append(xat);

                rootNode.AppendChild(xe);

            }

            doc.Save(configPath);

        }
    }


    public class flashconfig
    {
        private int autoPlayTime = 10;


        /// <summary>
        /// 自动切换时间间隔
        /// </summary>
        public int AutoPlayTime
        {
            get { return autoPlayTime; }
            set { autoPlayTime = value; }
        }

        private List<flashconfigitem> items;

        /// <summary>
        /// 
        /// </summary>
        public List<flashconfigitem> Items
        {
            get { return items; }
            set { items = value; }
        }

    }



    public class flashconfigitem : IComparable<flashconfigitem>
    {
        private string item_url = "webpic/images/default.jpg";

        /// <summary>
        /// 图片路径
        /// </summary>
        public string Item_url
        {
            get { return item_url; }
            set { item_url = value; }
        }

        private string title;

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return title; }
            set { title = value; }
        }


        private string link = "/";


        /// <summary>
        /// Url路径
        /// </summary>
        public string Link
        {
            get { return link; }
            set { link = value; }
        }


        private int id = 1;

        /// <summary>
        /// ID
        /// </summary>
        public int Id
        {
            get { return id; }
            set { id = value; }
        }



        #region IComparable<flashconfigitem> 成员

        public int CompareTo(flashconfigitem other)
        {
            return id.CompareTo(other.id);
        }

        #endregion
    }
}
